﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Speech.Recognition;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DeskApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //BackgroundWorker background =new BackgroundWorker ();
            // background.DoWork += Background_DoWork;
            // ListenHelper.Listen();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {

                //查看系统中都安装了哪些语言包

                foreach (RecognizerInfo ri in SpeechRecognitionEngine.InstalledRecognizers())
                {
                    string s = ri.Culture.Name;
                }


                SpeechRecognitionEngine sre = new SpeechRecognitionEngine(new CultureInfo("zh-CN"));//en-US

                //SpeechRecognitionEngine sre = new SpeechRecognitionEngine();
                var arr = new[] { 1, 3, 4, 5 };
                double pi = 3.1415_454;
                var obj = new { Age = "123" };
                int a = 3;
                if (obj is null)
                {

                }
                //命中关键字------------------------------------------------
                var words = new Choices();
                words.Add("喂");
                words.Add("你好");
                words.Add("院领导");
                words.Add("办公室");
                words.Add("办公区");
                words.Add("客户端咨询");
                words.Add("外网客户端咨询");
                words.Add("检品受理");
                words.Add("综合");
                words.Add("人事");
                words.Add("计划");
                words.Add("财务");

                words.Add("科研管理");
                words.Add("科研");
                words.Add("仪器");
                words.Add("设备");
                words.Add("网站");
                words.Add("网络安全");
                words.Add("信息");
                words.Add("档案");



                var gb = new GrammarBuilder();
                gb.Culture = new System.Globalization.CultureInfo("zh-CN");
                gb.Append(words);
                Grammar g = new Grammar(gb);

                sre.LoadGrammar(g);
                //------------------------------------------------------------------------

                //sre.LoadGrammar(new DictationGrammar());

                sre.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(SpeechRecognitionCallback);
                sre.SetInputToDefaultAudioDevice();
                sre.RecognizeAsync(RecognizeMode.Multiple);
            }
            catch (Exception ex)
            {
                MessageBox.Show("init RS Error : " + ex.ToString());
            }
        }



        // 识别的语音命令回调函数
        private static void SpeechRecognitionCallback(object sender, SpeechRecognizedEventArgs e)
        {
            string result = e.Result.Text.ToLower();

            MessageBox.Show(result);

        }
        /// <summary>
        /// tgg<see langword="bool"/>
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Background_DoWork(object sender, DoWorkEventArgs e)
        {

        }

        //<a href="call-demo://123"> 打开demo客户端 </a>
        //添加启动协议注册表
        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                Registry.ClassesRoot.DeleteSubKeyTree(@"gitwms");
            }
            catch (Exception ex)
            {
                Console.WriteLine("未找到注册的程序...");
            }

            string appPath = @"D:\\JHEcisExecuteTool\\JHEcisExecuteTool.exe"; // 你的应用程序路径

            //注册的协议头，即在地址栏中的路径 如cyrmt://xxxxx/xxx
            var surekamKey = Microsoft.Win32.Registry.ClassesRoot.CreateSubKey("gitwms");
            //以下这些参数都是固定的，不需要更改，直接复制过去
            var shellKey = surekamKey.CreateSubKey("shell");
            var openKey = shellKey.CreateSubKey("open");
            var commandKey = openKey.CreateSubKey("command");
            surekamKey.SetValue("URL Protocol", "");
            //这里可执行文件取当前程序全路径，可根据需要修改
            commandKey.SetValue("", "\"" + appPath + "\"" + " \"%1\"");
        }
    }
    [DebuggerDisplay("{Name},{Age}")]
    class TModel
    {

        public string Name { get; set; }
    }
}
